package com.one.group.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.SpChargeStandardsMapper;
import com.one.group.admin.mapper.SpHouseMapper;
import com.one.group.admin.mapper.SpMeterReadingdataMapper;
import com.one.group.model.dto.SpMeterReadingdataAddDto;
import com.one.group.model.dto.SpBillAddTestDto;
import com.one.group.model.dto.SpMeterReadingdataQueryDto;
import com.one.group.model.entity.SpChargeStandards;
import com.one.group.model.entity.SpMeterReadingdata;
import com.one.group.model.vo.SpBillPayerVo;
import com.one.group.model.vo.SpMeterReadingdataListVo;
import com.one.group.service.SpMeterReadingdataService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class SpMeterReadingdataServiceImpl extends ServiceImpl<SpMeterReadingdataMapper, SpMeterReadingdata> implements SpMeterReadingdataService {
    @Autowired
    private SpChargeStandardsMapper spChargeStandardsMapper;
    @Autowired
    private SpHouseMapper spHouseMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addMeterReadingdata(SpMeterReadingdataAddDto spMeterReadingdataAddDto) {
        try{
            //获取租户id
            Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
            //获取到收费标准数据
            SpChargeStandards spChargeStandards = spChargeStandardsMapper.selectOneById(spMeterReadingdataAddDto.getChargestandardId());
            //创建抄表数据对象
            SpMeterReadingdata spMeterReadingdata = new SpMeterReadingdata();
            spMeterReadingdata.setCellId(spMeterReadingdataAddDto.getCellId());
            spMeterReadingdata.setBuildingtypeId(spMeterReadingdataAddDto.getBuildingTypeId());
            spMeterReadingdata.setBuildingId(spMeterReadingdataAddDto.getBuildingId());
            spMeterReadingdata.setHouseNum(spHouseMapper.getHouseNumByBuildingIdAndHouseId(spMeterReadingdataAddDto.getBuildingId(),spMeterReadingdataAddDto.getHouseId()));
            spMeterReadingdata.setHouseId(spMeterReadingdataAddDto.getHouseId());
            spMeterReadingdata.setChargestandardId(spMeterReadingdataAddDto.getChargestandardId());
            spMeterReadingdata.setChargestandardName(spChargeStandards.getName());
            spMeterReadingdata.setUnitPrice(spChargeStandards.getPrice());
            spMeterReadingdata.setCurrentMonth(spMeterReadingdataAddDto.getCurrentMonth());
            spMeterReadingdata.setStartingDegree(spMeterReadingdataAddDto.getStartingDegree());
            spMeterReadingdata.setEndDegree(spMeterReadingdataAddDto.getEndDegree());
            spMeterReadingdata.setMagnification(spMeterReadingdataAddDto.getMagnification());
            if(save(spMeterReadingdata)){
                //提取账单添加信息
                SpBillAddTestDto spBillAddTestDto = new SpBillAddTestDto();
                SpBillPayerVo spBillPayerVo = spHouseMapper.getSpBillPayerVo(spMeterReadingdataAddDto.getHouseId());
                spBillAddTestDto.setPayerType(spBillPayerVo.getPayerType());
                spBillAddTestDto.setPayerId(spBillPayerVo.getPayerId());
                spBillAddTestDto.setChargestandardId(spMeterReadingdataAddDto.getChargestandardId());
                BigDecimal Amount = spMeterReadingdataAddDto.getEndDegree().subtract(spMeterReadingdataAddDto.getStartingDegree()).multiply(spMeterReadingdataAddDto.getMagnification()).multiply(spChargeStandards.getPrice());
                spBillAddTestDto.setAmount(Amount);
                spBillAddTestDto.setRemark(spChargeStandards.getRemarks());
                //发送消息
                rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
                rabbitTemplate.convertAndSend("gxa.direct","addBill",spBillAddTestDto);
                return true;
            }
            return false;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<SpMeterReadingdataListVo> queryList(SpMeterReadingdataQueryDto spMeterReadingdataQueryDto) {
        //获取当前租户id
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        return getMapper().queryList(spMeterReadingdataQueryDto.getName(),tenantId);
    }

    @Override
    public List<SpMeterReadingdataListVo> myExcelExport() {
        SpMeterReadingdataQueryDto spMeterReadingdataQueryDto = new SpMeterReadingdataQueryDto();
        spMeterReadingdataQueryDto.setPage(0);
        spMeterReadingdataQueryDto.setLimit(999);
        spMeterReadingdataQueryDto.setName("");
        return queryList(spMeterReadingdataQueryDto);
    }
}
